Buffer

Subclass of [class@Gtk.TextBuffer].

A GtkSourceBuffer object is the model for [class@View] widgets. It extends the [class@Gtk.TextBuffer] class by adding features useful to display and edit source code such as syntax highlighting and bracket matching.

To create a GtkSourceBuffer use [ctor@GtkSource.Buffer.new] or [ctor@GtkSource.Buffer.new_with_language]. The second form is just a convenience function which allows you to initially set a [class@Language]. You can also directly create a [class@View] and get its [class@Buffer] with [method@Gtk.TextView.get_buffer].

The highlighting is enabled by default, but you can disable it with [method@Buffer.set_highlight_syntax].

Context Classes:

It is possible to retrieve some information from the syntax highlighting engine. The default context classes that are applied to regions of a GtkSourceBuffer:

- **comment**: the region delimits a comment; - **no-spell-check**: the region should not be spell checked; - **path**: the region delimits a path to a file; - **string**: the region delimits a string.

Custom language definition files can create their own context classes, since the functions like [method@Buffer.iter_has_context_class] take a string parameter as the context class.

GtkSourceBuffer provides an API to access the context classes: [method@Buffer.iter_has_context_class], [method@Buffer.get_context_classes_at_iter], [method@Buffer.iter_forward_to_context_class_toggle] and [method@Buffer.iter_backward_to_context_class_toggle].

And the [signal@GtkSource.Buffer::highlight-updated] signal permits to be notified when a context class region changes.

Each context class has also an associated [class@Gtk.TextTag] with the name gtksourceview:context-classes:<name>. For example to retrieve the [class@Gtk.TextTag] for the string context class, one can write:

GtkTextTagTable *tag_table;
GtkTextTag *tag;

tag_table = gtk_text_buffer_get_tag_table (buffer);
tag = gtk_text_tag_table_lookup (tag_table, "gtksourceview:context-classes:string");

The tag must be used for read-only purposes.

Accessing a context class via the associated [class@Gtk.TextTag] is less convenient than the GtkSourceBuffer API, because:

- The tag doesn't always exist, you need to listen to the [signal@Gtk.TextTagTable::tag-added] and [signal@Gtk.TextTagTable::tag-removed] signals. - Instead of the [signal@GtkSource.Buffer::highlight-updated] signal, you can listen to the [signal@Gtk.TextBuffer::apply-tag] and [signal@Gtk.TextBuffer::remove-tag] signals.

A possible use-case for accessing a context class via the associated [class@Gtk.TextTag] is to read the region but without adding a hard dependency on the GtkSourceView library (for example for a spell-checking library that wants to read the no-spell-check region).

class Buffer : TextBuffer {}

Constructors

this
this(GtkSourceBuffer* gtkSourceBuffer, bool ownedRef)

Sets our main struct and passes it to the parent class.

this
this(TextTagTable table)

Creates a new source buffer.

this
this(Language language)

Creates a new source buffer using the highlighting patterns in language.

Members

Functions

addOnBracketMatched
gulong addOnBracketMatched(void delegate(TextIter, GtkSourceBracketMatchType, Buffer) dlg, ConnectFlags connectFlags)

@iter is set to a valid iterator pointing to the matching bracket if @state is %GTK_SOURCE_BRACKET_MATCH_FOUND. Otherwise @iter is meaningless.

addOnCursorMoved
gulong addOnCursorMoved(void delegate(Buffer) dlg, ConnectFlags connectFlags)

The "cursor-moved" signal is emitted when then insertion mark has moved.

addOnHighlightUpdated
gulong addOnHighlightUpdated(void delegate(TextIter, TextIter, Buffer) dlg, ConnectFlags connectFlags)

The ::highlight-updated signal is emitted when the syntax highlighting and context classes are updated in a certain region of the @buffer.

addOnSourceMarkUpdated
gulong addOnSourceMarkUpdated(void delegate(TextMark, Buffer) dlg, ConnectFlags connectFlags)

The ::source-mark-updated signal is emitted each time a mark is added to, moved or removed from the @buffer.

backwardIterToSourceMark
bool backwardIterToSourceMark(TextIter iter, string category)

Moves iter to the position of the previous [class@Mark] of the given category.

changeCase
void changeCase(GtkSourceChangeCaseType caseType, TextIter start, TextIter end)

Changes the case of the text between the specified iterators.

createSourceMark
Mark createSourceMark(string name, string category, TextIter where)

Creates a source mark in the buffer of category category.

ensureHighlight
void ensureHighlight(TextIter start, TextIter end)

Forces buffer to analyze and highlight the given area synchronously.

forwardIterToSourceMark
bool forwardIterToSourceMark(TextIter iter, string category)

Moves iter to the position of the next [class@Mark] of the given category.

getBufferStruct
GtkSourceBuffer* getBufferStruct(bool transferOwnership)

Get the main Gtk struct

getContextClassesAtIter
string[] getContextClassesAtIter(TextIter iter)

Get all defined context classes at @iter.

getHighlightMatchingBrackets
bool getHighlightMatchingBrackets()

Determines whether bracket match highlighting is activated for the source buffer.

getHighlightSyntax
bool getHighlightSyntax()

Determines whether syntax highlighting is activated in the source buffer.

getImplicitTrailingNewline
bool getImplicitTrailingNewline()
getLanguage
Language getLanguage()

Returns the [class@Language] associated with the buffer, see [method@Buffer.set_language].

getSourceMarksAtIter
ListSG getSourceMarksAtIter(TextIter iter, string category)

Returns the list of marks of the given category at @iter.

getSourceMarksAtLine
ListSG getSourceMarksAtLine(int line, string category)

Returns the list of marks of the given category at @line.

getStruct
void* getStruct()

the main Gtk struct as a void*

getStyleScheme
StyleScheme getStyleScheme()

Returns the [class@StyleScheme] associated with the buffer, see [method@Buffer.set_style_scheme].

iterBackwardToContextClassToggle
bool iterBackwardToContextClassToggle(TextIter iter, string contextClass)

Moves backward to the next toggle (on or off) of the context class.

iterForwardToContextClassToggle
bool iterForwardToContextClassToggle(TextIter iter, string contextClass)

Moves forward to the next toggle (on or off) of the context class.

iterHasContextClass
bool iterHasContextClass(TextIter iter, string contextClass)

Check if the class @context_class is set on @iter.

joinLines
void joinLines(TextIter start, TextIter end)

Joins the lines of text between the specified iterators.

removeSourceMarks
void removeSourceMarks(TextIter start, TextIter end, string category)

Remove all marks of @category between @start and @end from the buffer.

setHighlightMatchingBrackets
void setHighlightMatchingBrackets(bool highlight)

Controls the bracket match highlighting function in the buffer.

setHighlightSyntax
void setHighlightSyntax(bool highlight)

Controls whether syntax is highlighted in the buffer.

setImplicitTrailingNewline
void setImplicitTrailingNewline(bool implicitTrailingNewline)

Sets whether the @buffer has an implicit trailing newline.

setLanguage
void setLanguage(Language language)

Associates a [class@Language] with the buffer.

setStyleScheme
void setStyleScheme(StyleScheme scheme)

Sets a [class@StyleScheme] to be used by the buffer and the view.

sortLines
void sortLines(TextIter start, TextIter end, GtkSourceSortFlags flags, int column)

Sort the lines of text between the specified iterators.

Static functions

getType
GType getType()

Variables

gtkSourceBuffer
GtkSourceBuffer* gtkSourceBuffer;

the main Gtk struct

Inherited Members

From TextBuffer

gtkTextBuffer
GtkTextBuffer* gtkTextBuffer;

the main Gtk struct

getTextBufferStruct
GtkTextBuffer* getTextBufferStruct(bool transferOwnership)

Get the main Gtk struct

getStruct
void* getStruct()

the main Gtk struct as a void*

insertWithTags
void insertWithTags(TextIter iter, string text, TextTag[] tags)

Inserts text into buffer at iter, applying the list of tags to the newly-inserted text. The last tag specified must be NULL to terminate the list. Equivalent to calling gtk_text_buffer_insert(), then gtk_text_buffer_apply_tag() on the inserted text; gtk_text_buffer_insert_with_tags() is just a convenience function.

insertWithTagsByName
void insertWithTagsByName(TextIter iter, string text, string[] tags)

Same as gtk_text_buffer_insert_with_tags(), but allows you to pass in tag names instead of tag objects.

createTag
TextTag createTag(string tagName, ...)

Creates a tag and adds it to the tag table for buffer. Equivalent to adding a new tag to the buffer's tag table.

getText
string getText()

Obtain the entire text

getType
GType getType()
addMark
void addMark(TextMark mark, TextIter where)

Adds the mark at position @where.

addSelectionClipboard
void addSelectionClipboard(Clipboard clipboard)

Adds @clipboard to the list of clipboards in which the selection contents of @buffer are available.

applyTag
void applyTag(TextTag tag, TextIter start, TextIter end)

Emits the “apply-tag” signal on @buffer.

applyTagByName
void applyTagByName(string name, TextIter start, TextIter end)

Emits the “apply-tag” signal on @buffer.

backspace
bool backspace(TextIter iter, bool interactive, bool defaultEditable)

Performs the appropriate action as if the user hit the delete key with the cursor at the position specified by @iter.

beginIrreversibleAction
void beginIrreversibleAction()

Denotes the beginning of an action that may not be undone.

beginUserAction
void beginUserAction()

Called to indicate that the buffer operations between here and a call to gtk_text_buffer_end_user_action() are part of a single user-visible operation.

copyClipboard
void copyClipboard(Clipboard clipboard)

Copies the currently-selected text to a clipboard.

createChildAnchor
TextChildAnchor createChildAnchor(TextIter iter)

Creates and inserts a child anchor.

createMark
TextMark createMark(string markName, TextIter where, bool leftGravity)

Creates a mark at position @where.

cutClipboard
void cutClipboard(Clipboard clipboard, bool defaultEditable)

Copies the currently-selected text to a clipboard, then deletes said text if it’s editable.

delet
alias delet = delete_
Undocumented in source.
delete_
void delete_(TextIter start, TextIter end)

Deletes text between @start and @end.

deleteInteractive
bool deleteInteractive(TextIter startIter, TextIter endIter, bool defaultEditable)

Deletes all editable text in the given range.

deleteMark
void deleteMark(TextMark mark)

Deletes @mark, so that it’s no longer located anywhere in the buffer.

deleteMarkByName
void deleteMarkByName(string name)

Deletes the mark named @name; the mark must exist.

deleteSelection
bool deleteSelection(bool interactive, bool defaultEditable)

Deletes the range between the “insert” and “selection_bound” marks, that is, the currently-selected text.

endIrreversibleAction
void endIrreversibleAction()

Denotes the end of an action that may not be undone.

endUserAction
void endUserAction()

Ends a user-visible operation.

getBounds
void getBounds(TextIter start, TextIter end)

Retrieves the first and last iterators in the buffer, i.e. the entire buffer lies within the range [@start,@end).

getCanRedo
bool getCanRedo()

Gets whether there is a redoable action in the history.

getCanUndo
bool getCanUndo()

Gets whether there is an undoable action in the history.

getCharCount
int getCharCount()

Gets the number of characters in the buffer.

getEnableUndo
bool getEnableUndo()

Gets whether the buffer is saving modifications to the buffer to allow for undo and redo actions.

getEndIter
void getEndIter(TextIter iter)

Initializes @iter with the “end iterator,” one past the last valid character in the text buffer.

getHasSelection
bool getHasSelection()

Indicates whether the buffer has some text currently selected.

getInsert
TextMark getInsert()

Returns the mark that represents the cursor (insertion point).

getIterAtChildAnchor
void getIterAtChildAnchor(TextIter iter, TextChildAnchor anchor)

Obtains the location of @anchor within @buffer.

getIterAtLine
bool getIterAtLine(TextIter iter, int lineNumber)

Initializes @iter to the start of the given line.

getIterAtLineIndex
bool getIterAtLineIndex(TextIter iter, int lineNumber, int byteIndex)

Obtains an iterator pointing to @byte_index within the given line.

getIterAtLineOffset
bool getIterAtLineOffset(TextIter iter, int lineNumber, int charOffset)

Obtains an iterator pointing to @char_offset within the given line.

getIterAtMark
void getIterAtMark(TextIter iter, TextMark mark)

Initializes @iter with the current position of @mark.

getIterAtOffset
void getIterAtOffset(TextIter iter, int charOffset)

Initializes @iter to a position @char_offset chars from the start of the entire buffer.

getLineCount
int getLineCount()

Obtains the number of lines in the buffer.

getMark
TextMark getMark(string name)

Returns the mark named @name in buffer @buffer, or %NULL if no such mark exists in the buffer.

getMaxUndoLevels
uint getMaxUndoLevels()

Gets the maximum number of undo levels to perform.

getModified
bool getModified()

Indicates whether the buffer has been modified since the last call to [method@Gtk.TextBuffer.set_modified] set the modification flag to %FALSE.

getSelectionBound
TextMark getSelectionBound()

Returns the mark that represents the selection bound.

getSelectionBounds
bool getSelectionBounds(TextIter start, TextIter end)

Returns %TRUE if some text is selected; places the bounds of the selection in @start and @end.

getSelectionContent
ContentProvider getSelectionContent()

Get a content provider for this buffer.

getSlice
string getSlice(TextIter start, TextIter end, bool includeHiddenChars)

Returns the text in the range [@start,@end).

getStartIter
void getStartIter(TextIter iter)

Initialized @iter with the first position in the text buffer.

getTagTable
TextTagTable getTagTable()

Get the GtkTextTagTable associated with this buffer.

getText
string getText(TextIter start, TextIter end, bool includeHiddenChars)

Returns the text in the range [@start,@end).

insert
void insert(TextIter iter, string text)

Inserts @len bytes of @text at position @iter.

insertAtCursor
void insertAtCursor(string text)

Inserts @text in @buffer.

insertChildAnchor
void insertChildAnchor(TextIter iter, TextChildAnchor anchor)

Inserts a child widget anchor into the text buffer at @iter.

insertInteractive
bool insertInteractive(TextIter iter, string text, bool defaultEditable)

Inserts @text in @buffer.

insertInteractiveAtCursor
bool insertInteractiveAtCursor(string text, bool defaultEditable)

Inserts @text in @buffer.

insertMarkup
void insertMarkup(TextIter iter, string markup, int len)

Inserts the text in @markup at position @iter.

insertPaintable
void insertPaintable(TextIter iter, PaintableIF paintable)

Inserts an image into the text buffer at @iter.

insertRange
void insertRange(TextIter iter, TextIter start, TextIter end)

Copies text, tags, and paintables between @start and @end and inserts the copy at @iter.

insertRangeInteractive
bool insertRangeInteractive(TextIter iter, TextIter start, TextIter end, bool defaultEditable)

Copies text, tags, and paintables between @start and @end and inserts the copy at @iter.

moveMark
void moveMark(TextMark mark, TextIter where)

Moves @mark to the new location @where.

moveMarkByName
void moveMarkByName(string name, TextIter where)

Moves the mark named @name (which must exist) to location @where.

pasteClipboard
void pasteClipboard(Clipboard clipboard, TextIter overrideLocation, bool defaultEditable)

Pastes the contents of a clipboard.

placeCursor
void placeCursor(TextIter where)

This function moves the “insert” and “selection_bound” marks simultaneously.

redo
void redo()

Redoes the next redoable action on the buffer, if there is one.

removeAllTags
void removeAllTags(TextIter start, TextIter end)

Removes all tags in the range between @start and @end.

removeSelectionClipboard
void removeSelectionClipboard(Clipboard clipboard)

Removes a GdkClipboard added with gtk_text_buffer_add_selection_clipboard().

removeTag
void removeTag(TextTag tag, TextIter start, TextIter end)

Emits the “remove-tag” signal.

removeTagByName
void removeTagByName(string name, TextIter start, TextIter end)

Emits the “remove-tag” signal.

selectRange
void selectRange(TextIter ins, TextIter bound)

This function moves the “insert” and “selection_bound” marks simultaneously.

setEnableUndo
void setEnableUndo(bool enableUndo)

Sets whether or not to enable undoable actions in the text buffer.

setMaxUndoLevels
void setMaxUndoLevels(uint maxUndoLevels)

Sets the maximum number of undo levels to perform.

setModified
void setModified(bool setting)

Used to keep track of whether the buffer has been modified since the last time it was saved.

setText
void setText(string text)

Deletes current contents of @buffer, and inserts @text instead.

undo
void undo()

Undoes the last undoable action on the buffer, if there is one.

addOnApplyTag
gulong addOnApplyTag(void delegate(TextTag, TextIter, TextIter, TextBuffer) dlg, ConnectFlags connectFlags)

Emitted to apply a tag to a range of text in a GtkTextBuffer.

addOnBeginUserAction
gulong addOnBeginUserAction(void delegate(TextBuffer) dlg, ConnectFlags connectFlags)

Emitted at the beginning of a single user-visible operation on a GtkTextBuffer.

addOnChanged
gulong addOnChanged(void delegate(TextBuffer) dlg, ConnectFlags connectFlags)

Emitted when the content of a GtkTextBuffer has changed.

addOnDeleteRange
gulong addOnDeleteRange(void delegate(TextIter, TextIter, TextBuffer) dlg, ConnectFlags connectFlags)

Emitted to delete a range from a GtkTextBuffer.

addOnEndUserAction
gulong addOnEndUserAction(void delegate(TextBuffer) dlg, ConnectFlags connectFlags)

Emitted at the end of a single user-visible operation on the GtkTextBuffer.

addOnInsertChildAnchor
gulong addOnInsertChildAnchor(void delegate(TextIter, TextChildAnchor, TextBuffer) dlg, ConnectFlags connectFlags)

Emitted to insert a GtkTextChildAnchor in a GtkTextBuffer.

addOnInsertPaintable
gulong addOnInsertPaintable(void delegate(TextIter, PaintableIF, TextBuffer) dlg, ConnectFlags connectFlags)

Emitted to insert a GdkPaintable in a GtkTextBuffer.

addOnInsertText
gulong addOnInsertText(void delegate(TextIter, string, int, TextBuffer) dlg, ConnectFlags connectFlags)

Emitted to insert text in a GtkTextBuffer.

addOnMarkDeleted
gulong addOnMarkDeleted(void delegate(TextMark, TextBuffer) dlg, ConnectFlags connectFlags)

Emitted as notification after a GtkTextMark is deleted.

addOnMarkSet
gulong addOnMarkSet(void delegate(TextIter, TextMark, TextBuffer) dlg, ConnectFlags connectFlags)

Emitted as notification after a GtkTextMark is set.

addOnModifiedChanged
gulong addOnModifiedChanged(void delegate(TextBuffer) dlg, ConnectFlags connectFlags)

Emitted when the modified bit of a GtkTextBuffer flips.

addOnPasteDone
gulong addOnPasteDone(void delegate(Clipboard, TextBuffer) dlg, ConnectFlags connectFlags)

Emitted after paste operation has been completed.

addOnRedo
gulong addOnRedo(void delegate(TextBuffer) dlg, ConnectFlags connectFlags)

Emitted when a request has been made to redo the previously undone operation.

addOnRemoveTag
gulong addOnRemoveTag(void delegate(TextTag, TextIter, TextIter, TextBuffer) dlg, ConnectFlags connectFlags)

Emitted to remove all occurrences of @tag from a range of text in a GtkTextBuffer.

addOnUndo
gulong addOnUndo(void delegate(TextBuffer) dlg, ConnectFlags connectFlags)

Emitted when a request has been made to undo the previous operation or set of operations that have been grouped together.

Meta